package ec.gob.magap.era.web.general;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class Utilidades {
	
    private static final char[] CONSTS_HEX = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
    public static String encriptaEnMD5(String stringAEncriptar)
	{
		try
		{
		   MessageDigest msgd = MessageDigest.getInstance("MD5");
		   byte[] bytes = msgd.digest(stringAEncriptar.getBytes());
		   StringBuilder strbCadenaMD5 = new StringBuilder(2 * bytes.length);
		   for (int i = 0; i < bytes.length; i++)
		   {
		       int bajo = (int)(bytes[i] & 0x0f);
		       int alto = (int)((bytes[i] & 0xf0) >> 4);
		       strbCadenaMD5.append(CONSTS_HEX[alto]);
		       strbCadenaMD5.append(CONSTS_HEX[bajo]);
		   }
		   return strbCadenaMD5.toString();
		} catch (NoSuchAlgorithmException e) {
		   return null;
		}
	}
    
	private Utilidades(){
		
	}
	

	/**
	 * @return isPostBack
	 * M\u00E9todo que dice si una p\u00E1gina est\u00E1 realizando Postback o n\u00F3.
	 */
	public static boolean isPostBack() {
		 FacesContext facesContext = FacesContext.getCurrentInstance();
		 
		 return facesContext.getRenderKit().getResponseStateManager().isPostback(facesContext);
		}
	
	/**
	 * @return HttpSession
	 * M\u00E9todo que retorma el Objeto Session para setear o recuperar valores sobre \u00E9l.
	 */
	public static HttpSession getSession(){
		return (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	}
	
	/**
	 * @return HttpServletRequest
	 * M\u00E9todo que retorma el Objeto Request para setear o recuperar valores sobre \u00E9l.
	 */
	public static HttpServletRequest getRequest(){
		return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}
}




  	

	

